PHP新手30天實戰金流, Laravel今天的內容是依照
How to make an API with Laravel 的 Tutorial 4 & 5 來實作。
將關於 PersonController 的 request 都交由 apiResource 來處理。
Route::apiResource('/person', 'PersonController');
app/Http/Controllers/PersonController.php中,public function store(Request $request)
{
    $request->validate([
        'last_name' => 'required',
        'password'  => 'required',
        'email'     => 'required', 
    ]);
    $person = Person::create($request->all());
    return new PersonResource($person);
} 
app/Person.php中的Person Model裡,class Person extends Model
{
    protected $fillable = [
        'last_name',
        'email',
        'password',
    ];
}
選用 POST 方法
Body 的 tab 下選 form-data 格式,然後填充欄位和要 store的資料
按下 "Send" btn,下方有server回傳的資料就表示正確
到 DB 確認select id,last_name FROM people;

app/Http/Controllers/PersonController.php 中加 update涵式:public function update(Person $person, Request $request):PersonResource
    {
        $person->update($request->all());
        return new PersonResource($person);
    }
用 [postman]測試:
{
    "last_name":"day3_test_update"
}

3. 按下 "Send" btn,下方有server回傳的資料就表示正確
到 DB 確認select id,last_name FROM people;
app/Http/Controllers/PersonController.php 中加 destroy 函式:public function destroy(Person $person)
    {
        $person->delete();
        return response()->json();
    }
用 postman 測試:
選用 DELETE 方法, URL要指定更新的資料是哪一筆(用 primary key: id 指定)
選 Headers tab,下面欄位(Key, value)
 
若沒有勾會出現錯誤: The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST
按下 "Send" btn,下方有server回傳的"[]"資料就表示正確
到 DB 確認select id,last_name FROM people;

OK 今天先到這裡! 第四天要來學當我們欲更新 API 時,要如何做版本增加以利管理。
晚生學習分享所學經驗,若內容有誤或不清楚,煩請不吝指教!更是歡迎各位大神多多補充,感謝萬分!